<?php

namespace backend\controllers;

use backend\models\JobType;
use Yii;
use backend\models\search\JobSearch;
use backend\models\Job;
use backend\actions\CreateAction;
use backend\actions\UpdateAction;
use backend\actions\IndexAction;
use backend\actions\DeleteAction;
use backend\actions\SortAction;
use backend\actions\ViewAction;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;

/**
 * JobController implements the CRUD actions for Job model.
 */
class JobController extends \yii\web\Controller
{
    public function actions()
    {
        return [
            'index'      => [
                'class' => IndexAction::className(),
                'data'  => function () {

                    $searchModel  = new JobSearch();
                    $dataProvider = $searchModel->search(yii::$app->getRequest()->getQueryParams());

                    return [
                        'dataProvider' => $dataProvider,
                        'searchModel'  => $searchModel,
                    ];
                }
            ],
            'job'        => [
                'class'    => IndexAction::className(),
                'viewFile' => 'job',
                'data'     => function () {

                    $searchModel  = new JobSearch();
                    $dataProvider = $searchModel->searchJob(yii::$app->getRequest()->getQueryParams());

//                    var_dump($searchModel);
                    return [
                        'type'         => Job::PARTTIME_TYPE,
                        'dataProvider' => $dataProvider,
                        'searchModel'  => $searchModel,
                    ];
                }
            ],
            'mission'    => [
                'class'    => IndexAction::className(),
                'viewFile' => 'mission',
                'data'     => function () {

                    $searchModel  = new JobSearch();
                    $dataProvider = $searchModel->searchMission(yii::$app->getRequest()->getQueryParams());

                    return [
                        'type'         => Job::MISSION_TYPE,
                        'dataProvider' => $dataProvider,
                        'searchModel'  => $searchModel,
                    ];
                }
            ],
            'create'     => [
                'class'      => CreateAction::className(),
                'modelClass' => Job::className(),
            ],
            'update'     => [
                'class'           => UpdateAction::className(),
                'modelClass'      => Job::className(),
                'date'            => function () {
                    return [
                        'url' => Yii::$app->request->get('type') == JobType::TYPE_JOB ? Url::to(['job-type/job']) : Url::to(['job-type/mission'])
                    ];
                },
                'successRedirect' => Yii::$app->request->get('type') == JobType::TYPE_JOB ? Url::to(['job-type/job']) : Url::to(['job-type/mission']),
            ],
            'delete'     => [
                'class'      => DeleteAction::className(),
                'modelClass' => Job::className(),
            ],
            'sort'       => [
                'class'      => SortAction::className(),
                'modelClass' => Job::className(),
            ],
            'view-layer' => [
                'class'      => ViewAction::className(),
                'modelClass' => Job::className(),
            ],
        ];
    }
}
